//
//  ViewController.swift
//  httpsTest
//
//  Created by laoJun on 2016/12/20.
//  Copyright © 2016年 Kuick. All rights reserved.
//

import UIKit
import SafariServices

class ViewController: UIViewController,UIWebViewDelegate,URLSessionDelegate,NSURLConnectionDelegate,SFSafariViewControllerDelegate{

    var textField :UITextField?
    var webView :UIWebView?
    var request :URLRequest?
    var originRequest:URLRequest?
    var urlSession:URLSession?
    var urlTask:URLSessionTask?
     var authenticated:Bool = false
    var url :String?
    
    

    override func viewDidLoad() {
        super.viewDidLoad()
        webView = UIWebView.init(frame: self.view.bounds)
        webView!.delegate = self
        view.addSubview(webView!)
         url = "https://kuick.cn"
      
        webView?.loadRequest(getRequst(url!))
        
        addTextView()
        
        addWKButton()
        
        addS_Button()
        
       
    }
    
    func addS_Button(){
        let button = UIButton.init(frame: CGRect(x: view.frame.size.width - 108, y: view.frame.size.height - 30-30-10-30-10, width: 100, height: 30))
        button.addTarget(self, action: #selector(buttonClick), for: .touchUpInside)
        button.setTitle("safari(iOS 9.0)", for: UIControlState())
        button.tag = 888
        button.backgroundColor = UIColor.green
        view.addSubview(button)
    }
    
    
    func addWKButton(){
        let button = UIButton.init(frame: CGRect(x: view.frame.size.width - 68, y: view.frame.size.height - 30-30-10, width: 60, height: 30))
        button.addTarget(self, action: #selector(buttonClick), for: .touchUpInside)
        button.setTitle("wkweb", for: UIControlState())
        button.tag = 555
        button.backgroundColor = UIColor.green
        view.addSubview(button)
    }
    
    func getRequst(_ url:String)->URLRequest{
       let r =  URLRequest.init(url: URL.init(string: url)!)
        request = r
        originRequest = request

        return r
    }
    
    func addTextView(){
        
        textField = UITextField.init(frame: CGRect(x: 8, y: view.frame.size.height - 30, width: view.frame.size.width - 78, height: 30))
        textField?.backgroundColor =  UIColor.purple
        textField?.placeholder = "在此输入你的网址";
        view.addSubview(textField!)
        
        let button = UIButton.init(frame: CGRect(x: view.frame.size.width - 68, y: view.frame.size.height - 30, width: 60, height: 30))
        button.addTarget(self, action: #selector(buttonClick), for: .touchUpInside)
        button.setTitle("go", for: UIControlState())
        button.backgroundColor = UIColor.green
        view.addSubview(button)
        
    }

    
    func buttonClick(_ sender:UIButton){
        
        var urlString = textField?.text
        
        if urlString == "" {
            urlString = url
        }
        
        let request = getRequst(urlString!)
        
        if sender.tag == 0 {
             webView?.loadRequest(request)
        }else if (sender.tag == 555){
            
            let WKWeb = WKWebViewController(rquest: request)
            self.navigationController?.pushViewController(WKWeb, animated: true)
        }else{
            if #available(iOS 9.0, *) {
                let safari = SFSafariViewController.init(url:request.url! )
                safari.delegate = self
                self.navigationController?.pushViewController(safari, animated: true)
            
            } else {
                // Fallback on earlier versions
                let alert = UIAlertView.init(title: nil, message: "只能在ios9以上使用", delegate: nil, cancelButtonTitle: "艹")
                alert.show()
            }
            
        }
       
    }
    
    //MARK:webVeiw
    
    func webView(_ webView: UIWebView, didFailLoadWithError error: Error) {
         print(webView,error)
    }
    
    func webViewDidStartLoad(_ webView: UIWebView) {
         print(webView)
    }
    
    func webViewDidFinishLoad(_ webView: UIWebView) {
        print(webView)
    }
    
    func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
    
        
        
        return true
    }

    


    
    //MARK:SFSafariViewController delegate
    @available(iOS 9.0, *)
    func safariViewControllerDidFinish(_ controller: SFSafariViewController) {
          print(controller)
    }
    
    @available(iOS 9.0, *)
    func safariViewController(_ controller: SFSafariViewController, didCompleteInitialLoad didLoadSuccessfully: Bool) {
        print(controller,didLoadSuccessfully)
    }
    
    @available(iOS 9.0, *)
    func safariViewController(_ controller: SFSafariViewController, activityItemsFor URL: URL, title: String?) -> [UIActivity] {
        return [UIActivity.init()]
    }
    


}

